#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/16
# section 128
TITLE = "Clipboard"
DESCRIPTION = """
The Gtk.Clipboard object represents a clipboard of data shared between different processes
or between different widgets in the same process. Each clipboard is identified by a name encoded
as a Gdk.Atom. (Conversion to and from strings can be done with Gdk.Atom.intern() and Gdk.Atom.name().)
The default clipboard corresponds to the “CLIPBOARD” atom; another commonly used clipboard is the “PRIMARY” clipboard,
which, in X, traditionally contains the currently selected text.
"""
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk

items = ["edit-cut", "edit-paste", "edit-copy"]


class ClipboardWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Clipboard Example")

        grid = Gtk.Grid()

        # self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        atom = Gdk.atom_intern('CLIPBOARD', True)
        self.clipboard = Gtk.Clipboard.get(atom)
        # None:  all available targets should be saved.
        self.clipboard.set_can_store(None)
        self.entry = Gtk.Entry()

        button_copy_text = Gtk.Button("Copy Text")
        button_paste_text = Gtk.Button("Paste Text")
        grid.attach(self.entry, 0, 0, 1, 1)
        grid.attach(button_copy_text, 1, 0, 1, 1)
        grid.attach(button_paste_text, 2, 0, 1, 1)
        button_copy_text.connect("clicked", self.copy_text)
        button_paste_text.connect("clicked", self.paste_text)

        for i, item in enumerate(items):
            image = Gtk.Image.new_from_icon_name(item, Gtk.IconSize.MENU)
            button_copy_image = Gtk.Button("Copy Image")
            grid.attach(image, 0, i + 1, 1, 1)
            grid.attach(button_copy_image, 1, i + 1, 1, 1)
            button_copy_image.connect("clicked", self.copy_image, image)

        self.image = Gtk.Image()
        button_paste_image = Gtk.Button("Paste Image")
        grid.attach(self.image, 0, 4, 1, 1)
        grid.attach(button_paste_image, 1, 4, 2, 1)
        button_paste_image.connect("clicked", self.paste_image)

        self.add(grid)

    def copy_text(self, widget):
        self.clipboard.set_text(self.entry.get_text(), -1)

    def paste_text(self, widget):
        selection = self.clipboard.wait_for_contents(Gdk.atom_intern('UTF8_STRING', True))
        print(selection.get_text())
        text = self.clipboard.wait_for_text()
        if text is not None:
            self.entry.set_text(text)
        else:
            print("No text on the clipboard.")

    def copy_image(self, widget, image):
        image_type = image.get_storage_type()
        if image_type == Gtk.ImageType.PIXBUF:
            self.clipboard.set_image(image.get_pixbuf())
        elif image_type == Gtk.ImageType.ICON_NAME:
            name, size = image.get_icon_name()
            pixbuf = Gtk.IconTheme.get_default().load_icon(name, size, 0)
            self.clipboard.set_image(pixbuf)
        else:
            print("No image has been pasted yet.")

        # clear the image
        self.image.set_from_pixbuf(None)

    def paste_image(self, widget):
        # print(self.clipboard.wait_for_targets())
        selection = self.clipboard.wait_for_contents(Gdk.atom_intern('image/png', True))
        pixbuf = selection.get_pixbuf()
        # pixbuf = self.clipboard.wait_for_image()

        if pixbuf is not None:
            self.image.set_from_pixbuf(pixbuf)
        else:
            print("no image")


def main():
    win = ClipboardWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()
